"""
认证工具
提供JWT认证和权限控制相关的函数
"""
from functools import wraps
from flask import jsonify
from flask_jwt_extended import get_jwt_identity

from app.models.user import User, UserRole

def admin_required(f):
    """
    验证用户是否为管理员的装饰器
    """
    @wraps(f)
    def decorated_function(*args, **kwargs):
        current_user_id = get_jwt_identity()
        current_user = User.query.get(current_user_id)
        
        if not current_user or not current_user.is_admin():
            return jsonify({"msg": "需要管理员权限"}), 403
        
        return f(*args, **kwargs)
    
    return decorated_function 